package actions;

import gui.GUIWindow;

import java.awt.event.ActionEvent;
import java.util.Enumeration;

import server.RLM_Server;
import server.ServerSocketConnection;
import utils.Constants;

@SuppressWarnings("serial")
public class StopAction extends SysTrayAbstractAction {

	public StopAction(GUIWindow owner) {
		setActionOwner(owner);
		
//		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0));
//		putValue(SMALL_ICON, ImageCreator.createImageIcon(AboutAction.class, "images/stop.png"));
		putValue(NAME, "Stop");
		putValue(SHORT_DESCRIPTION, "Stop");
		
		setActionItem();
	}

	
	public void actionPerformed(ActionEvent e) {
		
		getActionOwner().getApplication().getRlmServerReference().stopServer();
		
		getActionOwner().getActionManager().setStoppedRights();
		
		/**
		 * Mora enumeracija zbog fail-fast-a.
		 */
		Enumeration<String> keys = RLM_Server.activeConnections.keys();
		while (keys.hasMoreElements()) {
			String key = keys.nextElement();
			ServerSocketConnection ssc = RLM_Server.activeConnections.get(key);
			String message = "SERV_LOGOUT " + Constants.LOGOUT_SEQUENCE;
			System.out.println("SENT: " + ssc.getUsername());
			ssc.setMessage(message);
			ssc.quitConnection();
		}
		
		getActionOwner().getApplication().getRlmServerReference().closeAllDBConnections();
		
//		for (ServerSocketConnection ssc : RLM_Server.activeConnections.values()) {
//			
//			String message = "SERV_LOGOUT " + Constants.LOGOUT_SEQUENCE;
//			System.out.println("SENT: " + ssc.getUsername());
//			ssc.setMessage(message);
//			ssc.quitConnection();
//		}
	}
	
}
